package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.entity.frontvo.VideoUrlVo;
import com.atguigu.eduservice.mapper.VideoMapper;
import com.atguigu.eduservice.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author AIR
 * @since 2022-09-20
 */
@Service
@Transactional
public class VideoServiceImpl extends ServiceImpl<VideoMapper, EduVideo> implements VideoService {

    //注入vodClient
    @Autowired
    private VodClient vodClient;

    //修改小节
    @Override
    public void updateVideo(String id,EduVideo eduVideo) {
        eduVideo.setId(id);
        baseMapper.updateById(eduVideo);
    }

    //1 根据课程id删除小节
    //TODO 删除小节，删除对应视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        //根据课程id查询课程所有的视频id
        QueryWrapper<EduVideo> wrapperVideoId = new QueryWrapper<>();
        wrapperVideoId.eq("course_id",courseId);
        wrapperVideoId.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wrapperVideoId);
        if (eduVideos != null){
            // List<EduVideo> 变成 List<String>
            List<String> videoIds = new ArrayList<>();
            for (int i = 0; i < eduVideos.size(); i++) {
                EduVideo eduVideo = eduVideos.get(i);
                String videoSourceId = eduVideo.getVideoSourceId();
                if (!StringUtils.isBlank(videoSourceId)){
                    //放到videoIds集合里面
                    videoIds.add(videoSourceId);
                }
            }

            if (videoIds.size() > 0){
                //根据多个视频id删除多个视频
                System.out.println(videoIds);
                vodClient.deleteBatch(videoIds);
            }
        }

        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        baseMapper.delete(wrapperVideo);
    }

    @Override
    public Boolean getVideoById(String id,EduVideo eduVideo) {
        eduVideo.setId(id);
        EduVideo video = baseMapper.selectById(id);
        if (!StringUtils.isBlank(video.getVideoSourceId())){
            vodClient.remove(video.getVideoSourceId());
            return true;
        }
        return false;
    }

    @Override
    public VideoUrlVo getPlayerAuth(String videoId) {
        EduVideo eduVideo = baseMapper.selectById(videoId);
        VideoUrlVo videoUrlVo = new VideoUrlVo();
        BeanUtils.copyProperties(eduVideo,videoUrlVo);
        return videoUrlVo;
    }
}
